#!/bin/sh

echo -n "Choose PREFIX (/usr): "
read PREFIX
[ "$PREFIX" = "" ] && PREFIX="/usr"
if [ ! -d "$PREFIX" ];then
 echo "ERROR: $PREFIX does not exist!"
 exit 1
fi
echo "Installing scripts into $PREFIX/sbin, libs in $PREFIX/lib"
[ ! -d ${PREFIX}/sbin ] && mkdir -p ${PREFIX}/sbin
[ ! -d ${PREFIX}/lib ] && mkdir -p ${PREFIX}/lib
for SCRIPT in `ls sbin`
do
 cat sbin/${SCRIPT} | sed "s*XXXX*$PREFIX*g" > ${PREFIX}/sbin/${SCRIPT}
 chmod +x ${PREFIX}/sbin/${SCRIPT}
done
[ -f ${PREFIX}/sbin/pkg-quiet ] && rm -f ${PREFIX}/sbin/pkg-quiet
ln -s ${PREFIX}/sbin/pkg ${PREFIX}/sbin/pkg-quiet
for LIB in `ls lib`
do
 cp -f lib/${LIB} ${PREFIX}/lib/${LIB}
done
if [ $PREFIX = $HOME ];then
 XDGDIR=$HOME/.local/share/applications
else
 XDGDIR=$PREFIX/share/applications
fi
cp -f share/gpkg.desktop $XDGDIR/gpkg.desktop
mkdir -p $PREFIX/share/package-tools/locale 2>/dev/null
cp -f share/package-tools/locale/* $PREFIX/share/package-tools/locale/
echo -n "Location for Gpkg (/usr/local/apps/System): "
read LOCATION
[ "$LOCATION" = "" ] && LOCATION="/usr/local/apps/System"
cp -af apps/Gpkg ${LOCATION}/
echo '#!/bin/sh' > ${PREFIX}/sbin/gpkg
echo -n "exec ${LOCATION}/Gpkg/AppRun " >> ${PREFIX}/sbin/gpkg
echo '"$@"' >> ${PREFIX}/sbin/gpkg
chmod ugo+x ${PREFIX}/sbin/gpkg
echo "Package Tools are installed"
